package cn.ym.mall.service.impl;

import cn.ym.mall.dao.ShopTrolleyDao;
import cn.ym.mall.dao.impl.ShopTrolleyDaoImpl;
import cn.ym.mall.entity.ShopTrolley;
import cn.ym.mall.service.ShopTrolleyService;
import cn.ym.mall.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 购物车 service 层实现类
 */
public class ShopTrolleyServiceImpl implements ShopTrolleyService {
    private ShopTrolleyDao shopTrolleyDao = new ShopTrolleyDaoImpl();

    /**
     * 新增购物车
     *
     * @param shopTrolley 添加的购物车
     * @return 返回成功的条数
     */
    @Override
    public boolean addShopTrolley(ShopTrolley shopTrolley) throws SQLException {
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection(false);
            if (shopTrolleyDao.addShopTrolley(shopTrolley) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 根据购物车编号删除购物车
     *
     * @param stid 购物车
     * @return 返回删除条数
     */
    @Override
    public boolean deleteByStid(Integer stid) throws SQLException {
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection(false);
            if (shopTrolleyDao.deleteByStid(stid) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 查询所有购物车信息
     *
     * @return 购物车信息
     * @throws SQLException
     */
    @Override
    public List<ShopTrolley> getAllShopTrolley() throws SQLException {
        List<ShopTrolley> list = new ArrayList<>();
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            shopTrolleyDao.getAllShopTrolley();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return list;
    }

    /**
     * 根据购物车编号查询购物车信息
     *
     * @param stid
     * @return
     */
    @Override
    public ShopTrolley getShopTrolleyByStid(Integer stid) throws SQLException {
        ShopTrolley shopTrolley =null;
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            shopTrolley = shopTrolleyDao.getShopTrolleyByStid(stid);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return shopTrolley;
    }

    /**
     * 根据商品编号统计购物车商品数量
     *
     * @param gid 商品编号
     * @return 返回商品数量
     * @throws SQLException
     */
    @Override
    public long getCountByGid(Integer gid) throws SQLException {
        int num = 0;
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            num = (int) shopTrolleyDao.getCountByGid(gid);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return num;
    }

    /**
     * 修改购物车
     *
     * @param shopTrolley 购物车
     * @return 返回更新成功的条数
     */
    @Override
    public boolean updateShopTrolley(ShopTrolley shopTrolley) throws SQLException {
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection(false);
             if (shopTrolleyDao.updateShopTrolley(shopTrolley) == 1){
                DruidUtils.commit();
                return true;
             }else {
                 DruidUtils.rollback();
             }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }
}
